﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Geofencing.Resources;
using System.Device.Location;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Maps.Toolkit;
using Windows.Devices.Geolocation.Geofencing;
using Windows.Devices.Geolocation;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

namespace Geofencing
{
    public partial class MainPage : PhoneApplicationPage
    {
        GeoCoordinateWatcher gps;
        MapLayer layer = new MapLayer();
        MapOverlay overlay = new MapOverlay();
        GeofenceMonitor monitor = GeofenceMonitor.Current;
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            gps = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            gps.Start();

            gps.PositionChanged += gps_PositionChanged;

            Pushpin myPin = new Pushpin();
            
            overlay.GeoCoordinate = gps.Position.Location;
            overlay.Content = myPin;

            Pushpin msPin = new Pushpin();
            MapOverlay msOverlay = new MapOverlay();
            msOverlay.GeoCoordinate = new GeoCoordinate(13.7390, 100.5482);
            msOverlay.Content = msPin;

            layer.Add(msOverlay);
            layer.Add(overlay);

            MyMap.Layers.Add(layer);
            MyMap.Center = gps.Position.Location;
            MyMap.ZoomLevel = 14;

            BasicGeoposition msPosition = new BasicGeoposition();
            msPosition.Latitude = 13.7390;
            msPosition.Longitude = 100.5482;
            
            //Geofence Initial
            Geofence msArea = new Geofence("microsoft", new Geocircle(msPosition, 100));
            try
            {
                monitor.Geofences.Add(msArea);
            }
            catch
            {

            }

            monitor.GeofenceStateChanged += monitor_GeofenceStateChanged;
            

        }

        void monitor_GeofenceStateChanged(GeofenceMonitor sender, object args)
        {
            var getReport = sender.ReadReports();
            foreach(var report in getReport)
            {
                if(report.Geofence.Id == "microsoft")
                {
                    if(report.NewState == GeofenceState.Entered)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                            {
                                var toastNotifier = ToastNotificationManager.CreateToastNotifier();
                                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
                                var toastText = toastXml.GetElementsByTagName("text");
                                (toastText[0] as XmlElement).InnerText = "GeofencingMIC15";
                                (toastText[1] as XmlElement).InnerText = "Welcome to Microsoft";
                                var toast = new ToastNotification(toastXml);
                                toastNotifier.Show(toast);
                                //MessageBox.Show("Welcome to Microsoft");
                            });
                       
                    }
                    else if(report.NewState == GeofenceState.Exited)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            var toastNotifier = ToastNotificationManager.CreateToastNotifier();
                            var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
                            var toastText = toastXml.GetElementsByTagName("text");
                            (toastText[0] as XmlElement).InnerText = "GeofencingMIC15";
                            (toastText[1] as XmlElement).InnerText = "GoodBye";
                            var toast = new ToastNotification(toastXml);
                            toastNotifier.Show(toast);
                        });
                    }
                }
            }
        }

        void gps_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            overlay.GeoCoordinate = gps.Position.Location;
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}